package example;

import java.util.List;
import java.util.Map;

import com.easyfun.easyframe.msg.MsgUtil;

/**
 * 测试程序：使用SimpleConsumer API
 * 
 * @author linzhaoming
 *
 * @Created 2014
 */
public class TestSimpleConsumer {
	private static String topic2 = "topic2";
	private static String topic3 = "topic3";
	
	public static class ProducerThread extends Thread {
		private final String topic;

		public ProducerThread(String topic) {
			this.topic = topic;
		}

		public void run() {
			int messageNo = 1;
			while (true) {
				MsgUtil.send(topic, "Message_" + messageNo);
				messageNo++;
			}
		}
	}

	private static void generateData() {
		ProducerThread producer2 = new ProducerThread(topic2);
		producer2.start();
		ProducerThread producer3 = new ProducerThread(topic3);
		producer3.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		 generateData();
		System.out.println("Testing single fetch");
		List<String> list1 = MsgUtil.fetch(topic2, 0, 0, 100);
		for (String str : list1) {
			System.out.println(str);
		}

		System.out.println("Testing single multi-fetch");
		Map<String, List<String>> fetchResponse2 = MsgUtil.fetch(new String[] { topic2, topic3 }, 0, 0, 100);

		for (String topic : fetchResponse2.keySet()) {
			List<String> list = fetchResponse2.get(topic);
			System.out.println("Topic: " + topic);
			for (String str : list) {
				System.out.println(str);
			}
		}
		
		MsgUtil.shutdownConsummer();
	}
	
}
